<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable @register="registerTable" class="w-5/5 xl:w-6/6" :searchInfo="searchInfo">
    </BasicTable>
  </PageWrapper>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import { BasicTable, useTable, TableAction } from '@/components/Table'
import { PageWrapper } from '@/components/Page'
import OrgTree from './OrgTree.vue'
import { useModal } from '@/components/Modal'
import AccountModal from './AccountModal.vue'
import AccountAssignModal from './AccountAssignModal.vue'
import { getClassifyTreeByDeviceCode } from '@/api/collection/classify'

defineOptions({ name: 'deviceCollectionManager' })

const searchInfo = reactive<Recordable>({})
const [registerTable, { reload, updateTableDataRecord }] = useTable({
  title: '设备节点列表',
  api: getClassifyTreeByDeviceCode,
  rowKey: 'id',
  size: 'small',
  columns: [{
    title: '设备节点',
    dataIndex: 'classifyName',
    format: (val) => val.join('>>'),
    align: 'left'
  }],
  pagination: false,
  formConfig: {
    labelWidth: 120,
    schemas: [
        {
            label: '设备编码',
            field: 'deviceCode',
            component: 'Input',
            colProps: { span: 6 },
        }
    ],
    autoSubmitOnEnter: true,
  },
  immediate: false,
  useSearchForm: true,
  showTableSetting: false,
  bordered: true,
  handleSearchInfoFn(info) {
    // console.log('handleSearchInfoFn', info)
    return info
  },
})

function handleCreate() {
  openModal(true, {
    isUpdate: false,
  })
}

function handleAssign(record: Recordable) {
  // console.log(record)
  openAssignModal(true, {
    record,
    isUpdate: true,
  })
}

function handleEdit(record: Recordable) {
  // console.log(record)
  openModal(true, {
    record,
    isUpdate: true,
  })
}

async function handleDelete(record: Recordable) {
  await deleteUser(record.userId)
  reload();
}

function handleSuccess({ isUpdate, values }) {
  if (isUpdate) {
    // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
    updateTableDataRecord(values.id, values)
  } else {
    reload()
  }
}

function handleAssignSuccess() {
  // console.log('handle assign success');
  reload();
}

function handleSelect(orgId = '') {
  searchInfo.orgId = orgId
  reload()
}

async function handleResetPassword(record: Recordable) {
  await resetPassword(record.userId)
}

</script>
